<?php
/**
 * link: http://www.bjmaimeng.com/
 * copyright: Copyright (c) 2018 北京麦盟科技有限公司
 * author: jack_guo
 */

namespace app\forms\mall\export;

use app\core\CsvExport;
use app\models\CoreFile;
use app\plugins\community\models\Goods;

class CommunityStatisticsExport extends BaseExport
{

    public function fieldsList()
    {
        return [
            [
                'key' => 'name',
                'value' => '商品名称',
            ],
            [
                'key' => 'user_num',
                'value' => '拼团人数',
            ],
            [
                'key' => 'pay_user_num',
                'value' => '支付人数',
            ],
            [
                'key' => 'pay_goods_num',
                'value' => '支付件数',
            ],
            [
                'key' => 'pay_price',
                'value' => '支付金额',
            ],
            [
                'key' => 'activity_status',
                'value' => '状态',
            ],
        ];
    }

    public function export($query = null)
    {
        \Yii::warning('导出开始');
        try {
            $list = $this->query->asArray()->all();

            foreach ($list as &$item) {
                $item['name'] = Goods::findOne($item['goods_id'])->getName();
            }

            $this->getFields();
            // 文件夹唯一标识
            $id = \Yii::$app->mall->id . '_' . $this->mch_id;
            // 唯一文件名称
            $fileName = sprintf('%s%s%s%s', $this->getFileName(), $id, time(), '.csv');

            $coreFile = new CoreFile();
            $coreFile->mall_id = \Yii::$app->mall->id;
            $coreFile->mch_id = $this->mch_id;
            $coreFile->file_name = $fileName;

            $this->transform($list);
            $dataList = $this->getDataList();
            (new CsvExport())->newAjaxExport($dataList, $this->fieldsNameList, $fileName, $id);

            $coreFile->status = 1;
            $coreFile->percent = 1;
            $res = $coreFile->save();
            if (!$res) {
                throw new \Exception($this->getErrorMsg($coreFile));
            }

            \Yii::warning('导出结束');
        }catch(\Exception $exception) {
            \Yii::error('导出异常');
            \Yii::error($exception);

            $coreFile->status = 2;
            $coreFile->save();
        }
    }

    public function getFileName()
    {
        return '社区团购统计';
    }

    protected function transform($list)
    {
        $newList = [];
        $arr = [];

        $number = 1;
        foreach ($list as $key => $item) {
            $arr['number'] = $number++;
            $item['user_num'] = intval($item['user_num']);
            $item['pay_user_num'] = intval($item['pay_user_num']);
            $item['pay_goods_num'] = intval($item['pay_goods_num']);
            $item['pay_price'] = floatval($item['pay_price']);

            $arr = array_merge($arr, $item);

            $newList[] = $arr;
        }
        $this->dataList = $newList;
    }

    protected function getFields()
    {
        $arr = [];
        foreach ($this->fieldsList() as $key => $item) {
            $arr[$key] = $item['key'];
        }
        $this->fieldsKeyList = $arr;
        parent::getFields(); // TODO: Change the autogenerated stub
    }
}
